/*
 * Copyright (c) 2025 HiSilicon (Shanghai) Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef OT_AF_TUNING_INFO_H
#define OT_AF_TUNING_INFO_H

#include <stdint.h>
#include "ot_type.h"

#define OT_AF_MAX_ENTRIES 10000
#define OT_AF_CTRL_MAX_ENTRIES 100

typedef enum {
    OT_AF_MONOTONIC_INCREASE,
    OT_AF_MONOTONIC_DECREASE,
} OT_AF_Monotonicity;

typedef struct {
    td_u32      iso;
    td_u32      minConf;
} OT_AF_TuningPDConf;

typedef struct {
    /* 倍率 需要单调递增 */
    td_float               zoomRatio;
    td_u32                 numConfs; // 由外部分配  最大100
    OT_AF_TuningPDConf     *confs;
} OT_AF_TuningPDConfEntry;

typedef struct {
    td_u32                      numEntries;
    /* 外部分配 最大 100  00 */
    OT_AF_TuningPDConfEntry     *entries;
} OT_AF_TuningPDConfTable;

/* PD 触发对焦 灵敏度 */
typedef struct {
    td_bool     enableStableCtrl;
    /* [0,50] Number of frames to wait after focus search finishes
       before detecting out-of-focus (or defocus) condition
    */
    td_u32      waitAfterFocusCnt;
    /* [0, 50] Maximum size of frame-to-frame difference in temporally filtered defocus
    above which defocus is deemed not stable. Focus convergence can only be triggered if defocus is deemed stable */
    td_u32      defocusStableThreshold;
    /* [0, 20] Size of moving average filter applied to defocus sample */
    td_u32      defocusStableFilterLength;
} OT_AF_TuningPDFocusTrigger;

/* PD 收敛、判断合焦阈值  */
typedef struct {
    td_u32      iso;
    /* [1,100] Minimum parallel scan step size to use for contrast-based parallel scan following PDAF focus converge */
    td_u32      parallelSearchStepSize;
    /* [1,50] Minimum fine scan step size to use for contrast-based fine scan following PDAF focus converge  */
    td_u32      fineSearchStepSize;
    /* [0,50] Minimum size of absolute defocus below which lens is assumed to be in focus  */
    td_u32      focusDoneThreshold;
    /* [0, 50] Maximum size of frame-to-frame difference in temporally filtered defocus
    above which defocus is deemed not stable. Focus convergence can only be triggered if defocus is deemed stable */
    td_u32      defocusStableThreshold;
    /* [0, 20] Size of moving average filter applied to defocus sample */
    td_u32      defocusStableFilterLength;
} OT_AF_TuningPDFocusConverge;

typedef struct {
    /* 倍率 需要单调递增 */
    td_float                        zoomRatio;
    /* 由外部分配  最大100 */
    td_u32                          numCtrls;
    OT_AF_TuningPDFocusConverge     *ctrls;
} OT_AF_TuningPDFocusConvergeEntry;

/* PD 收敛、判断合焦阈值  */
typedef struct {
    /* [0,10] Number of frames to wait for confidence level to recover from a temporary drop (e.g. scene change)  */
    td_u32                              waitConfRecoverCnt;
    td_bool                             enableFineSearch;
    td_u32                              numEntries;
    /* 外部分配 最大 100  00 */
    OT_AF_TuningPDFocusConvergeEntry    *entries;
} OT_AF_TuningPDFocusConvergeTable;

typedef struct {
    OT_AF_TuningPDConfTable              confTable;
    OT_AF_TuningPDFocusConvergeTable     focusConvergeTable;
} OT_AF_TuningPDAF;

/* TOF 置信度阈值 */
typedef struct {
    td_u32      minConf;
} OT_AF_TuningDAConfTable;

/* TOF 触发对焦 灵敏度 */
typedef struct {
    /* [0,1000] Distance in mm difference treated as scene change */
    td_u32      sceneChangeDistanceThreshold;
} OT_AF_TuningDAFocusTrigger;

/* TOF 收敛、判断合焦深度阈值  */
typedef struct {
    /* [0,10] Number of frames to wait for confidence level to recover from a temporary drop (e.g. scene change)  */
    td_u32      waitConfRecoverCnt;
    /* [0,1] Enable contrast-based fine scan following DAAF focus convergence  */
    td_bool     enableFineSearch;
    /* [1,50] Minimum fine scan step size to use for contrast-based fine scan following DAAF focus converge  */
    td_u32      fineSearchStepSize;
    /* [0,50] Minimum size of absolute defocus below which lens is assumed to be in focus  */
    td_u32      focusDoneThreshold;
} OT_AF_TuningDAFocusConverge;

typedef struct {
    OT_AF_TuningDAConfTable         confTable;
    OT_AF_TuningDAFocusTrigger      focusTrigger;
    OT_AF_TuningDAFocusConverge     focusConverge;
} OT_AF_TuningDAAF;

/* PDAF/DAAF算法搜索优先级 */
typedef enum {
    OT_ALG_SEARCH_PRIORITY_NONE = 0,
    OT_ALG_SEARCH_PRIORITY_PDAF,
    OT_ALG_SEARCH_PRIORITY_DAAF,
    OT_ALG_SEARCH_PRIORITY_BUTT
} OT_AF_TuningAlgSearchPrio;

typedef struct {
    OT_AF_TuningAlgSearchPrio algPriority;
} OT_AF_TuningHAF;

/* 马达平滑度控制 */
typedef struct {
    /* Defocus in logical lens position unit range=[0x0,0xFFFFFFFF] */
    td_u32      defocus;
    /* Percentage lens movement to target lens position range=[0,1] */
    td_float    movePercent;
} OT_AF_TuningFocusSmoothCtrl;

typedef struct {
    /* 倍率需要单调递增 */
    td_float                        zoomRatio;
    td_u32                          numCtrls;
    /* 对应倍率下的控制列表 defocus 需要严格单调递增，movePercent range=[0.00,1.00]，否则控制不起作用              */
    OT_AF_TuningFocusSmoothCtrl     *ctrls; // 最大 100
} OT_AF_TuningFocusSmoothCtrlEntry;

typedef struct {
    /* 是否使能对焦平滑控制 */
    td_bool                          enableFocusSmoothCtrl;
    td_u32                           numEntries;
    OT_AF_TuningFocusSmoothCtrlEntry *entries; // 最大 10000
} OT_AF_TuningFocusSmoothCtrlTable;

/* 搜索范围限制 search limits */
typedef struct {
    td_float    zoomRatio;          /* 倍率需要单调递增 */
    td_s32      nearEnd;            /* Nearest position lens can move to */
    td_s32      farEnd;             /* Farest position lens can move to */
} OT_AF_TuningFocusSearchLimits;

typedef struct {
    td_u32                            numEntries;
    OT_AF_TuningFocusSearchLimits     *entries; // 最大 10000
} OT_AF_TuningFocusSearchLimitsTable;

/* 变倍对焦曲线 */
typedef struct {
    /* 倍率 */
    td_float    zoomRatio;
    /* 焦距 */
    td_float    focalLength;
    /* zoom 马达位置 */
    td_u32      zoomPos;
    /* [1, 100] 对应对焦距离下的对焦马达位置 */
    td_u32      numFocusPos;
    td_s32      *focusPos;
} OT_AF_TuningZoomFocusCtrl;

typedef struct {
    /* 是否使能变倍对焦控制 */
    td_bool                     enableZoomFocusCtrl;
    /* [1, 100] 有效变倍对焦距离个数 */
    td_u32                      numfocusDistance;
    /* 变倍对焦距离列表 */
    td_float                    *focusDistance;
    /* [1, 10000] 有效变倍对焦控制项个数 */
    td_u32                      numEntries;
    /* 变倍对焦控制项列表 */
    OT_AF_TuningZoomFocusCtrl   *zoomFocusEntries;
} OT_AF_TuningZoomFocusCtrlTable;

/* 展示模式tuning配置参数 */
typedef struct {
    td_u32      iso;
    /* 选择联通区域的置信度阈值 */
    td_u32      minConnectedConf;
    /* 选择联通区域的弱纹理FV 阈值 */
    td_u32      connectedFlatSceneFvThreshold;
    /* 选择联通区域的通过FV 判定弱纹理的 defocus阈值 */
    td_u32      connectedFvDefocusThreshold;
    /* 选择联通区域的左右PP 图的亮度差 阈值 */
    td_float    connectedPdLumaDiffThreshold;
    /* [1,10] 前景窗中最小联通区域个数 */
    td_u32      minConnectedObjectCnt;
    /* 联通区域的离焦量差值阈值 */
    td_u32      connectedDiffThreshold;
    /* [0,50] 前景模式的合焦离焦量阈值 */
    td_u32      focusDoneThreshold;
    /* 联通小框defocus差值在focusDoneThreshold 范围内的个数    */
    td_u32      minWindowInDoFCnt;
    /* 联通小框PdLumaDiff 超过 connectedPdLumaDiffThreshold 的比例    */
    td_float    minWindowPdLumaDiffRatio;
    /* Size of moving average filter applied to minWindowInDoFCnt stable sample */
    td_u32      foregroundStableFilterLength;
    /* [0, 20] Size of moving average filter applied to window stable sample */
    td_u32      windowStableFilterLength;
    /* 绝对信任置信度阈值，确保联通区域无弱纹理 */
    td_u32      trustMinConf;
    /* 退化到预制框时，预制框中覆盖的联通小框和预制框defocus差值在focusDoneThreshold 范围内的个数    */
    td_u32      connectedInPrecastDiffCnt;
    /* 联通区域的离焦量 和预制灵活框的离焦量差值阈值 */
    td_u32      connectedToPrecastFlexDiffThreshold;
    /* 前景预制框，可用的置信度阈值 */
    td_u32      preCastWindowMinConf;
    /* 前景预制框 弱纹理FV 阈值 */
    td_u32      preCastWindowFlatSceneFvThreshold;
    /* 前景预制框通过FV 判定弱纹理的 defocus阈值 */
    td_u32      preCastWindowFvDefocusThreshold;
    /* 前景预制框的左右PP 图的亮度差 阈值 */
    td_float    preCastWindowPdLumaDiffThreshold;
    /* 前景最大单框，方向可信的置信度阈值 */
    td_u32      maxWindowMinConf;
    /* 前景单框 弱纹理FV 阈值 */
    td_u32      maxWindowFlatSceneFvThreshold;
    /* 前景单框通过FV 判定弱纹理的 defocus阈值 */
    td_u32      maxWindowFvDefocusThreshold;
    /* 前景单框 的左右PP 图的亮度差 阈值 */
    td_float    maxWindowPdLumaDiffThreshold;
    /* 前景模式的合焦离焦量阈值 */
    td_u32      minDefocus;
    /* [1,1000] 合焦后的判断离焦的focusDoneThreshold倍数 */
    td_u32      defocusDoneThresholdMultiple;
} OT_AF_TuningExhibitMode;

typedef struct {
    /* 倍率 需要单调递增 */
    td_float                        zoomRatio;
    /* 由外部分配  最大100 */
    td_u32                          numCtrls;
    OT_AF_TuningExhibitMode         *ctrls;
} OT_AF_TuningExhibitModeEntry;

typedef struct {
    td_bool                             enableWindowStableCtrl;
    /* 物距inf到macro对应准焦code的单调性 */
    OT_AF_Monotonicity                  codeMonotonicity;
    td_u32                              numEntries;
    /* 外部分配 最大 100  00 */
    OT_AF_TuningExhibitModeEntry        *entries;
} OT_AF_TuningExhibitModeTable;

/* cd 粗搜进入合焦下判断需要是否重新对焦，需要综合fv变化和y分量变化 */
typedef struct {
    /* [1, 1000] 用以区分fv变化上限不同方式的阈值 */
    td_u32 cdafFvThreadhold;
    /* [1, 500] 合焦fv小于cdafFvThreadhold，fv变化小于该值则保持合焦 */
    td_u32 cdafFvDiffMaxWhenInfocus;
    /* [0.0, 1.0] 合焦fv大于等于cdafFvThreadhold，fv变化小于该比例*合焦fv则保持合焦 */
    td_float cdafFvDiffRatioMaxWhenInfocus;
    /* [0.0, 1.0] Y分量变化小于该比例*合焦Y则保持合焦 */
    td_float cdafYDiffRatioMaxWhenInfocus;
} OT_AF_TuningCdafParallelInfocus;

/* cd 粗搜进入 pd 条件 */
typedef struct {
    /* [1, 1000] Pdaf 信任置信度，高于此值cd可直接转pd */
    td_u32 pdConfCdToPd;
    /* (0.0, 5.0] Pdaf 置信度一般时，小于此值cd可直接转pd */
    td_float pdValCdToPd;
    /* [1, 1000] 从cdaf粗搜进入Pdaf 后再次进入cdaf粗搜后 pdaf绝对信任置信度，高于此值cd可以转pd */
    td_u32 pdConfCdafParallelToPd;
} OT_AF_TuningCdafParallelToPdaf;

/* Tuning parameters */
typedef struct {
    OT_AF_TuningPDAF        pdafTuningData;
    OT_AF_TuningDAAF        daafTuningData;
    OT_AF_TuningHAF         hafTuningData;
    OT_AF_TuningExhibitModeTable exhibitModeTable;
    OT_AF_TuningZoomFocusCtrlTable zoomFocusCtrlTable;
    OT_AF_TuningFocusSmoothCtrlTable focusSmoothCtrlTable;
    OT_AF_TuningFocusSearchLimitsTable focusSearchLimitsTable;
} OT_AF_TuningInfo;

/* Dynamic Tuning parameters */
typedef struct {
    /* 是否使能精搜过程中能被pdaf打断 */
    td_bool enableFineSearchToPdaf;
    td_bool enablePdFocusConvergeStableCtrl;
    /* 是否使能前景合焦状态下通过FV变化判断离焦并直接切到pdaf */
    td_bool enableFvDefocusToPdaf;
    OT_AF_TuningPDFocusTrigger focusTrigger;
    OT_AF_TuningCdafParallelInfocus cdParallelInfocusTuning;
    OT_AF_TuningCdafParallelToPdaf cdParallelToPdTuning;
} OT_AF_DynamicTuningInfo;
#endif
